package com.zhiyume.oss;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.util.OSSClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class OssControl1 {
    @GetMapping(value="/ossUpload1")
    public String ossUpload(){
        return "ossUpload1";
    }

    /**
     * 上传至阿里云OSS
     * @param file
     * @param request
     * @param response
     * @throws FileNotFoundException
     */
    @PostMapping(value="/ossUpload1")
    public void ossUpload(@RequestParam(value="file")MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {

        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String uploadFlag = "0";

        if (!file.isEmpty()) {
            try {
                BufferedImage image = ImageIO.read(file.getInputStream());
                fileName = DateUtil.date2String(new Date(), DateUtil.DATE_PATTERN.YYYYMMDDHHMMSS) + "_" + fileName;

//                OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
//                String uploadStr = AliyunOSSClientUtil.uploadObject2OSS(ossClient,nFile,OSSClientConstants.BUCKET_NAME,"drama/");
//                String imgUrl = AliyunOSSClientUtil.getUrl(ossClient,OSSClientConstants.BUCKET_NAME,"");

                OSSClientUtil ossClientUtil = new OSSClientUtil();
                String md5Str = ossClientUtil.uploadImg2Oss(file);
                String imgUrl = ossClientUtil.getUrl("");

                System.out.println(">>>>>>>>>>上传文件至阿里云返回串<<<<<<<<<"+md5Str);

                System.out.println("------图片上传至阿里云的访问地址为---------" + imgUrl);
                /*
                示例：如您在Region为华北2，Bucketname为zhiyume-img，Object为drama/aaa.png，那么您的外网访问地址为：
                zhiyume-img.oss-cn-beijing.aliyuncs.com/drama/aaa.png
                您还可以直接将Object的URL链接放入HTML中使用，如下所示：
                <img src="https://zhiyume-img.oss-cn-beijing.aliyuncs.com/drama/aaa.png" />
                 */

                // 返回页面
                // 返回JSON结构如下:
                //{"height":418,"width":535,"pathInfo":"http://zhiyume-img.oss-cn-beijing.aliyuncs.com/data/20171016173556_2222.png?Expires=1823506557&OSSAccessKeyId=LTAI8hTtxsI962e5&Signature=plJ2F7CdDslVCzUm3yBqoUlilN8%3D"}
                Map<String,Object> data = new HashMap<String, Object>();
                data.put("pathInfo",imgUrl);
                data.put("width",image.getWidth());
                data.put("height",image.getHeight());
                System.out.println("---------图片宽度-----"+image.getWidth()+"-------图片高度----"+image.getHeight());
                ObjectMapper mapper = new ObjectMapper();
                String ret = mapper.writeValueAsString(data);

                // 返回一个json字符串
                response.setContentType("text/html;charset=utf8");
                response.getOutputStream().write(ret.getBytes());
                response.flushBuffer();

            } catch (Exception e) {
                System.out.println("--------------------------------123456798--------------------------------");
                e.printStackTrace();
            }

        } else {
            //return "上传失败，因为文件是空的.";
        }
    }


    public static void main(String[] args) throws FileNotFoundException {
        URL url = ResourceUtils.getURL("classpath:static/uploadImg/");
        System.out.println(url.getPath());
        System.out.println("123");
    }
}